import api from '../api/index'
import Vuex from 'vuex'

const initStore = () => {
  const state = {
    api,
    token: '',
    areaCode: '',
    user: {},
    source: {},
    // 用户信息是否加载
    loaded: false,
    myGroupInfo: {},
    ruleList: []
  };
  const store = new Vuex.Store({
    state,
    mutations: {
      // 保存token 和 areaCode ， 所有组件都可以获取到
      changeToken(state, payload) {
        state.token = payload.token
      },
      changeAreaCode(state, payload) {
        state.areaCode = payload.areaCode
      },
      changeUser(state, payload) {
        state.user = payload.user
      },
      changeSource(state, payload) {
        state.loaded = true
        state.source = payload.source
      },
      setMyGroupInfo(state, payload) {
        state.myGroupInfo = payload.myGroupInfo
      },
      setRuleList(state, payload) {
        state.ruleList = payload.ruleList
      }
    },
    getters: {},
    actions: {
      getUserInfo({ commit, state }) {
        state.api.homePage.getTeamAndPlayers().then(res => res.json()).then(data => {
          console.log(data)
          if (data.errorCode) {
            return
          }
          const source = data.source;
          if(source) {
            localStorage.setItem('userinfo', JSON.stringify({
              userinfo: source.user
            }))
            commit('changeSource', { source })
            commit('changeUser', { user: source.user })
          }
          
        })
      },
      queryGroupInfo({ commit, state }) {
        state.api.group.getMyGroupInfo().then(res => res.json()).then(data => {
          console.log(data);
          if (data.result) {
            return
          }
          const myGroup = data.data
          commit('setMyGroupInfo', { myGroupInfo: myGroup })
          const params = {
            crowdId: myGroup.id,
            offset: 0,
            limit: 100
          }
          state.api.group.getRuleList(params).then(res => res.json()).then(data => {
            console.log(data);
            if (data.result) {
              return
            }
            const ruleList = data.data.dataList
            ruleList.forEach(rule => {
              switch (rule.gameType) {
                case 1:
                  rule._appendInfo = classicBullRules(JSON.parse(rule.jsonContent))
                  break
                case 2:
                  rule._appendInfo = fightBullRules(JSON.parse(rule.jsonContent))
                  break
                case 5:
                  rule._appendInfo = dianziBullRules(JSON.parse(rule.jsonContent))
                  break
                case 3:
                  rule._appendInfo = majiangRules(JSON.parse(rule.jsonContent))
                  break
              }
            })
            console.log(ruleList);
            commit('setRuleList', { ruleList })
          })
        })

        function classicBullRules(rule) {
          // 玩法：0斗公牛；1抢庄；2牛牛庄家；3牌大坐庄；4轮庄；5霸王庄
          const ruleTextList = ['斗公牛', '抢庄', '牛牛庄家', '牌大坐庄', '轮庄', '霸王庄']
          const ruleText = ruleTextList[rule.ruleType]
          const visibleText = '可见牌' + rule.showCardCount + '张'
          const playerLimit = rule.maxPlayerCount === -1 ? '人数不限' : rule.maxPlayerCount + '人'
          return {
            ruleName: `经典牛牛/${ruleText}/${visibleText}/${playerLimit}`,
            headUrl: 'img/playerGroup/bull-icon.png',
          }
        }

        function fightBullRules(rule) {
          const visibleText = '可见牌' + rule.showCardCount + '张'
          const playerLimit = rule.maxPlayerCount === -1 ? '人数不限' : rule.maxPlayerCount + '人'
          return {
            ruleName: `斗公牛/${visibleText}/${playerLimit}`,
            headUrl: 'img/playerGroup/bull-icon.png',
          }
        }

        function dianziBullRules(rule) {
          const bullName = rule.ruleType === 6 ? '牛牛几倍' : '牛牛相差'
          const visibleText = '可见牌' + rule.showCardCount + '张'
          const playerLimit = rule.maxPlayerCount === -1 ? '人数不限' : rule.maxPlayerCount + '人'
          return {
            ruleName: `点子牛(${bullName})/${visibleText}/${playerLimit}`,
            headUrl: 'img/playerGroup/bull-icon.png',
          }
        }

        function majiangRules(rule) {
          // 玩法：0斗公牛；1抢庄；2牛牛庄家；3牌大坐庄；4轮庄；5霸王庄
          const ruleTextList = ['一码全中', '两个码', '四个码', '六个码', '八个码']
          const ruleText = ruleTextList[rule.ruleType - 1]
          const playerLimit = rule.maxPlayerCount === -1 ? '人数不限' : rule.maxPlayerCount + '人'
          return {
            ruleName: `红中麻将/${ruleText}/${rule.turns * 8 + '回合'}/${playerLimit}`,
            headUrl: 'img/playerGroup/majiang-icon.png'
          }
        }
      }
    }
  });

  return store;
};

export default initStore;